package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ProjectsService;
import com.ruoyi.web.controller.common.CaptchaController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ProjectsService projectsService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CaptchaController captchaController;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        Boolean a;
        if(!"admin".equals(loginBody.getUsername())){
            a=sysUserService.getDaeptId(loginBody.getDeptId(),loginBody.getUsername())>0;
        }else {
            a=true;
        }
        // 生成令牌
        if(a) {
            AjaxResult ajax = AjaxResult.success();
            String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }else {
            return  AjaxResult.error("该公司未存在此用户"+loginBody.getUsername());
        }
    }
    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        if(projectsService.selectDeptId(user.getUserId()+"")>0) {
            for (int i = 0; i < menus.size(); i++) {
                if (menus.get(i).getMenuId()==1) {
                    for (int j = 0; j < menus.get(i).getChildren().size(); j++) {
                        if (menus.get(i).getChildren().get(j).getMenuId() ==103) {
                            menus.get(i).getChildren().remove(j);
                        }
                    }
                }
            }
        }
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
